package com.four.service.impl;

import com.four.entity.Role;
import com.four.mapper.RoleMapper;
import com.four.service.RoleService;
import com.four.vo.Result;
import com.four.vo.RoleVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: qy158-system
 * @description:
 * @author: 闫克起2
 * @create: 2022-10-11 10:34
 **/
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Override
    public Result findByUserId(Integer adminId) {
        //1.查询所有的角色
        List<Role> list= roleMapper.selectAll();
        //2.查询用户具有的角色id
        List<String> roleIds= roleMapper.findRoleIdByUserId(adminId);

        Map<String,Object> map=new HashMap<>();
        map.put("list",list);
        map.put("roleIds",roleIds);

        return new Result(200,"查询成功",map);
    }

    @Override
    public Result qr(Integer roleId, Integer[] pIds) {
        //1.根据用户删除该用户具有的角色id
        roleMapper.deleteByUserId(roleId);
        //2.添加新的角色id
        roleMapper.insertRoleId(roleId,pIds);

        return new Result(200,"添加成功");
    }

    @Override
    public Result findByCondition(Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<Role> roles=roleMapper.selectByCondition();
        PageInfo<Role> pageInfo=new PageInfo<>(roles);
        return new Result(200,"查询成功",pageInfo);
    }

    @Override
    public Result updata(Role role) {
        roleMapper.updateByPrimaryKey(role);
        return new Result(200,"修改成功");
    }

    @Override
    public Result delete(Integer roleId) {
        roleMapper.deleteByPrimaryKey(roleId);
        return new Result(200,"删除成功");
    }

    @Override
    public Result addRole(Role role) {
        int insert = roleMapper.insert(role);
        return new Result(200,"添加成功",insert);
    }
}
